const express = require('express');
const router = express.Router();
const R = require("../util/json")
const connection = require("../util/db")
const json = require("../util/jsonFormat")

function extracted(record, sql, objs) {
    if(record.roleId !== 1){
        sql+="and driver_id = ? ";
        objs.push(record.loginId)
    }
    if(record.type){
        sql+="and type = ? ";
        objs.push(record.type)
    }
    if (record.status) {
        sql += "and r.status = ?";
        objs.push(record.status);
    }
    if (record.timeRange) {
        const tr = record.timeRange.split(",");
        const startTime = tr[0];
        const endTime = tr[1];
        // sql+="and start_time > ? and end_time < ?";//整个记录时间包含在内
        sql += "and start_time between ? and ? ";//只匹配开始时间范围
        objs.push(startTime, endTime);
        /*假设2月修车5月结束修车，查询范围是1月到3月
        * 方案1则无记录方案2可查到
        * */
    }
    return sql;
}

router.get("/page",function (req, res) {
    const record = req.query;
    record.roleId=req.roleId;
    record.loginId = req.loginId;
    const {page=1,limit=5}=req;
    let objs = [];
    let sql = "select r.*,u.nickname driverName from record r left join userinfo u on r.driver_id = u.id where 1= 1 ";
    sql = extracted(record, sql, objs);
    sql+="order by id desc limit ?,?";
    objs.push((page-1)*limit,parseInt(limit));
    connection.query(sql,objs,function (e,recordList,meta) {
        if(e){
            throw e;
        }
        sql="select count(r.id) count from record r left join userinfo u on r.driver_id = u.id where 1= 1 ";
        objs=[];
        sql=extracted(record,sql,objs);
        connection.query(sql,objs,function (e,r) {
            if(e){
                throw e;
            }
            res.json(R.page(r[0].count,json.formatArray(recordList,meta)))
        })
    })
})
router.get("/info/:id",function (req,res) {
    connection.query("select * from record where id = ?",[req.params.id],function (e,r,meta) {
        if(e){
            throw e;
        }
        if(r.length === 1){
            res.json(R.data(json.formatObject(r[0],meta)))
        }else{
            res.json(R.error("编号异常"))
        }
    })
})

router.post("/status",function (req, res) {
    const {id,status} = req.body;
    connection.query("update record set status = ? where id = ?",[status,id],function (e) {
        if(e){
            throw e;
        }
        res.json(R.ok());
    })
})

router.delete("/delete/:id",function (req, res) {
    connection.query("delete from record where id = ?",[req.params.id],function (e) {
        if(e){
            throw e;
        }
        res.json(R.ok());
    })
})

router.post("/update",function (req, res) {
    const {id,type,startTime,endTime,content,status,money} = req.body;
    if(id){
        let sql ="update record set start_time = ?,end_time = ?,content =?,status = ?,money = ? where id = ?";
        const objs=[startTime,endTime,content,status,money,id];
        connection.query(sql,objs,function (e) {
            if(e){
                throw e;
            }
            res.json(R.ok());
        })
    }else{
        const driverId = req.loginId;
        let sql = "insert into record values(null,?,?,?,?,?,?,?)";
        const objs = [type,startTime,endTime,content,driverId,status,money];
        connection.query(sql,objs,function (e) {
            if(e){
                throw e;
            }
            res.json(R.ok());
        })
    }
})


module.exports = router;
